﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'userRoleDelete')
	BEGIN
		DROP  Procedure  userRoleDelete
	END
GO

-- =============================================
-- Author:		Ricardo
-- Create date: 04/03/2009
-- Description:	Deletes a role from a user
-- =============================================
CREATE PROCEDURE [dbo].[userRoleDelete] 
	@user_name VARCHAR(50),
	@role_name VARCHAR(50),
	@app_id INT
AS	
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	DECLARE @rol_id INT;
	DECLARE @usr_id INT;

	SELECT @rol_id = lit_rol_id
	FROM lit_rol
	WHERE lit_rol_nombre = @role_name
	AND lit_aplicacion_id = @app_id;

	SELECT @usr_id = lit_usuario_id
	FROM lit_usuario
	WHERE lit_usuario_username = @user_name
	AND lit_aplicacion_id = @app_id;

	IF ((@rol_id IS NOT NULL) AND (@usr_id IS NOT NULL))
	BEGIN
		DELETE FROM lit_usuario_rol 
		WHERE lit_rol_id = @rol_id 
		AND lit_usuario_id = @usr_id;
		SELECT 0;
	END
	ELSE BEGIN
		SELECT 1; 
	END
END
GO